<template>
	<view>
		<view style="text-align: center; margin-top: 150rpx;">
			<image src="../../static/ypi.jpg" style="width: 150rpx; height: 150rpx;"></image>
		</view>
		<view style="text-align: center; margin-top: 30rpx;">
			<text>欢迎使用智慧停车微信小程序</text>
		</view>
		<view style="margin: 20rpx;">
			<view class="myinput" >
				<input name="username" placeholder="请输入账号" v-model="username" />
			</view>
			<view class="myinput">
				<input name="password" placeholder="请输入密码" password="true" v-model="password"/>
			</view>					
			<button style="margin-top: 30rpx;" @click="logincheck()">登录</button>
		</view>
		
	</view>
</template>

<script>
	import {loginurl} from '../../common/api.js'
	export default {
		data() {
			return {
				username:"",
				password:""
			}
		},
		onShow() {
			let jwt = '';
			jwt = uni.getStorageSync('jwt');
			if(jwt!='')
			{
				uni.switchTab({
					url: '/pages/parkinginfo/parkinginfo'
				});
			}
		},
		
		methods: {
			logincheck()
			{
				//console.log("你点击了登录按钮");
				if(this.username==""||this.password=="")
				{
					uni.showToast({
						title: '账号或密码不能为空',
						icon:"none"
						
					});
				}
				else
				{
					uni.showLoading({
						title: '登录中'
					});
					
					uni.request({
						url:loginurl,
						method:'POST',
						timeout:3000,
						data:{
							phone: this.username,
							pwd:this.password
						},
						header:{
							'Content-Type':'application/x-www-form-urlencoded'
						},
						success: (res) => {
							let result = res.data;
							console.log(result)
							let status = result.status;
							if(status==0)
							{
								uni.showToast({
									title: '登录成功',
									icon:"none"								
								});
								let jwt = result.data.jwt;
								uni.setStorageSync('jwt',jwt);
								
								uni.switchTab({
									url: '/pages/parkinginfo/parkinginfo'
								});
							}
							else
							{
								uni.showToast({
									title: result.msg,
									icon:"none"								
								});
							}
							
						},
						fail: () => {
							uni.showToast({
								title: '网络异常',
								icon:"none"								
							});
						}
					})
				}
			}
		}
	}
</script>

<style>
	.myinput{
		background-color: #FFFFFF;
		height: 50rpx;
		padding: 15rpx;
		line-height: 50rpx;
		margin-top: 3rpx;
		
		
	}

</style>
